package dic.persistence;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dic.model.City;
/*
 * 
 * 获得字典的DAO类
 */
public class DicDaoImpl extends HibernateDaoSupport implements DicDao {

	

	 /**
     * 
     * 获得省
     * @return 返回省列表
     */
	public List getProvince() {
		List listProvince=this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				Query query =session.createQuery("from Province p order by p.id");
				return query.list();
			}
			
		});
		return listProvince;
	}
	/**
     * 
     * 查找city
     */
	public City findCityByName(String name) {
		
		List list=this.getHibernateTemplate().find("from City city where city.name like '%"+name+"%'");
		City city=(City)list.get(0);
		return city;
	}
	/**
	 * 获得city by id
	 */
	public City findCityById(int id) {
		City city=(City)this.getHibernateTemplate().get(City.class, id);
		return city;
	}
    /**
     * 获得城市列表
     */
  
	public List getCityList(final int pid) {
		List listCity=this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				Query query =session.createQuery("from City city where city.pid="+pid+" order by city.id");
				return query.list();
			}
			
		})	;
		return listCity;
	}
    /**
     * 开通城市
     */
	public void dredge(final int id) {
		this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				Query query =session.createQuery("update City city set city.flag=1 where city.id="+id);
				return query.executeUpdate();
			}
		});
	}
	 /**
     * 开通城市
     */
	public List getDredgeCitys() {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				Query query =session.createQuery("from City city  where city.flag=1");
				return query.list();
			}
		});
		return list;
	}
	/**
	 * 获得Category
	 */
	public List getCategoryList(final int flag) {
		List list =(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				Query query =session.createQuery(" from Category c where c.sort="+flag);
				return query.list();
			}
			
		});
		return list;
	}
	/*
	 * 获得category by id List(non-Javadoc)
	 * @see dic.persistence.DicDao#getProvinceCityByIdForList(java.lang.String)
	 */
	public List getProvinceCityByIdForList(final String categoryId) {
		List list =(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				Query query =session.createQuery(" from Category c where c.id in("+categoryId+")");
				return query.list();
			}
		});
		return list;
	}
	/**
	 * 获得品牌用户类型列表
	 */
	public List getCategoryList(final String categoryId) {
		List list =(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				Query query =session.createQuery(" from Category c where c.id in("+categoryId+")");
				return query.list();
			}
		});
		return list;
	}
}
